/*
 * Copyright (C) Jahia Ltd. All rights reserved.
 *
 * This software is published under the terms of the Jahia Open Software
 * License version 1.1, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 */
package org.jahia.sqlprofiler.gui;

import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import java.net.URL;
import java.io.InputStream;
import java.io.IOException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * <p>Title: SQL Profiler</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Jahia Ltd</p>
 * @author Serge Huber
 * @author Jean-Philippe Valentin
 * @version 1.0
 *
 * 11/14/2003 : Add button to save table profile statistics as report file CSV by Jean-Philippe VALENTIN (JPV)
 */

public class ToolbarPanel extends JPanel {
    private static final org.apache.log4j.Logger logger = org.apache.log4j.
        Logger.getLogger(ToolbarPanel.class);

    BorderLayout borderLayout1 = new BorderLayout();

    private JButton playButton;
    private JButton pauseButton;
    private JButton reportButton;  //jpv : Add a button to save SQL table profile statistics generated in a file
    private boolean playing = true;

    private LoggerTableModel loggerModel;
    private ProfileStatementTableModel profileStatementTableModel;

    public ToolbarPanel(LoggerTableModel loggerModel, ProfileStatementTableModel profileStatementTableModel) {
        this.loggerModel = loggerModel;
        this.profileStatementTableModel = profileStatementTableModel;
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    void jbInit() throws Exception {
        this.setLayout(borderLayout1);
        JToolBar toolbar = new JToolBar();
        java.net.URL saveAsIconURL = ClassLoader.getSystemResource("toolbarButtonGraphics/general/SaveAs16.gif");
        Icon saveAsIcon = new ImageIcon(saveAsIconURL, "Save As");
        JButton myButton = new JButton(saveAsIcon);
        myButton.setToolTipText("Save the generated SQL INDEX File as...");
        myButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent aEvent) {
                JFileChooser chooser = new JFileChooser();
                // Note: source for ExampleFileFilter can be found in FileChooserDemo,
                // under the demo/jfc directory in the Java 2 SDK, Standard Edition.
                chooser.setDialogTitle("Save Index SQL File As...");
                int returnVal = chooser.showSaveDialog(null);
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    profileStatementTableModel.saveSQLIndexFile(chooser.getSelectedFile().toString());
                }
            }
        });
        toolbar.add(myButton);
        toolbar.addSeparator();
        java.net.URL clearIconURL = ClassLoader.getSystemResource("toolbarButtonGraphics/general/Delete16.gif");
        Icon clearIcon = new ImageIcon(clearIconURL, "Clear");
        myButton = new JButton(clearIcon);
        myButton.setToolTipText("Clear all the log entries from memory");
        myButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent aEvent) {
                loggerModel.clear();
            }
        });
        toolbar.add(myButton);
        toolbar.addSeparator();
        java.net.URL playIconURL = ClassLoader.getSystemResource("toolbarButtonGraphics/media/Play16.gif");
        Icon playIcon = new ImageIcon(playIconURL, "Play");
        playButton = new JButton(playIcon);
        playButton.setEnabled(false);
        playButton.setToolTipText("Resume real-time display and gather SQL statistics");
        playButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent aEvent) {
                loggerModel.toggle();
                togglePlayButtons();
            }
        });
        toolbar.add(playButton);
        java.net.URL pauseIconURL = ClassLoader.getSystemResource("toolbarButtonGraphics/media/Pause16.gif");
        Icon pauseIcon = new ImageIcon(pauseIconURL, "Pause");
        pauseButton = new JButton(pauseIcon);
        pauseButton.setToolTipText("Pause real-time display and display SQL table profile statistics");
        pauseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent aEvent) {
                loggerModel.toggle();
                togglePlayButtons();
            }
        });
        toolbar.add(pauseButton);
// JPV Add a button to save SQL table profile statistics generated in a file
        java.net.URL reportIconURL = ClassLoader.getSystemResource("toolbarButtonGraphics/general/Edit16.gif");
        Icon reportIcon = new ImageIcon(reportIconURL, "Report");
        reportButton = new JButton(reportIcon);
        reportButton.setEnabled(false);
        reportButton.setToolTipText("Save SQL table profile statistics generated in a file ...");
        reportButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent aEvent) {
                JFileChooser chooser = new JFileChooser();
                // Note: source for ExampleFileFilter can be found in FileChooserDemo,
                // under the demo/jfc directory in the Java 2 SDK, Standard Edition.
                chooser.setDialogTitle("Save Report File As...");
                int returnVal = chooser.showSaveDialog(null);
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    profileStatementTableModel.saveReportFile(chooser.getSelectedFile().toString());
                }
            }
        });
        toolbar.addSeparator();
        toolbar.add(reportButton);
//End JPV
        add(toolbar, BorderLayout.NORTH);
    }

    public void togglePlayButtons() {
        playing = !playing;
        if (playing) {
            playButton.setEnabled(false);
            pauseButton.setEnabled(true);
            reportButton.setEnabled(false);  //JPV : set to false the button to save SQL table profile statistics generated in a file
        } else {
            playButton.setEnabled(true);
            pauseButton.setEnabled(false);
            reportButton.setEnabled(true);  //JPV : set to true the button to save SQL table profile statistics generated in a file
        }
    }

}